package com.demo.kotlin.ui.mine

import android.os.Bundle
import android.text.Editable
import com.demo.kotlin.BuildConfig
import com.demo.kotlin.R
import com.demo.kotlin.databinding.ActivityLoginBinding
import com.demo.kotlin.ui.mine.mvp.ILoginView
import com.demo.kotlin.ui.mine.mvp.LoginPresenter
import com.ptt.jituangou.widgets.listener.TextWatcherImpl
import com.puhui.lib.base.MvpBaseActivity
import com.puhui.lib.permission.PermissionUtils
import com.puhui.lib.permission.Permissions
import com.puhui.lib.utils.getSignatureInfo
import com.puhui.lib.utils.setClick
import com.puhui.lib.widgets.TextViewUtils

class LoginActivity : MvpBaseActivity<ActivityLoginBinding, LoginPresenter>(), ILoginView {

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter(this)
    }

    override fun init(savedInstanceState: Bundle?) {
        //申请权限
        PermissionUtils.checkNeededPermission(
            this,
            Permissions.READ_WRITE_PHONE_PERMISSION,
            object : PermissionUtils.CheckPermissionsCallBack {
                override fun onGenerated() {
                }
            })

        mContentView.accountNameEt.addTextChangedListener(object : TextWatcherImpl() {
            override fun afterTextChanged(s: Editable) {
                checkInputInfo()
            }
        })

        mContentView.passwordEt.addTextChangedListener(object : TextWatcherImpl() {
            override fun afterTextChanged(s: Editable) {
                checkInputInfo()
            }
        })

        mContentView.passwordEyeCb.setOnCheckedChangeListener { buttonView, isChecked ->
            TextViewUtils.showOrHintEditTextPwd(mContentView.passwordEt, isChecked)
        }

        mContentView.loginTv.setClick {
            mPresenter.doLogin(mContentView.accountNameEt.text.toString(), mContentView.passwordEt.text.toString())
        }

        if (BuildConfig.DEBUG) {
            getSignatureInfo(this)
            mContentView.passwordEt.setText("a123456")
            mContentView.accountNameEt.setText("790548749@qq.com")
        }
    }

    /**
     * 判断登录按钮是否可点击
     */
    private fun checkInputInfo() {
        mContentView.loginTv.isEnabled = false
        if (mContentView.accountNameEt.length() >= 4 && mContentView.passwordEt.length() >= 6) {
            mContentView.loginTv.isEnabled = true
        }
    }

    override fun onLoginSuccess() {

    }
}